package com.the_last.controller;

import com.the_last.utils.OSSUtil;
import com.the_last.utils.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/file")
@RequiredArgsConstructor
public class FileController {
    
    private final OSSUtil ossUtil;
    
    /**
     * 上传MultipartFile文件
     *
     * @param file 文件
     * @param type 文件类型，可选值：venue, product
     * @return 文件访问路径
     */
    @PostMapping("/upload")
    @PreAuthorize("hasAuthority('FILE_UPLOAD')")
    public Result<String> upload(@RequestParam("file") MultipartFile file, 
                                @RequestParam(value = "type", defaultValue = "venue") String type) {
        try {
            // 根据类型决定存储目录
            String directory;
            switch (type.toLowerCase()) {
                case "product":
                    directory = "product";
                    break;
                case "venue":
                default:
                    directory = "venue";
                    break;
            }
            
            String fileUrl = ossUtil.uploadFile(file, directory);
            return Result.success(fileUrl);
        } catch (Exception e) {
            return Result.fail("文件上传失败：" + e.getMessage());
        }
    }
}